﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mozlite.Properties;

namespace Mozlite
{
    /// <summary>
    /// 参数判断类型。
    /// </summary>
    public static class Guard
    {
        /// <summary>
        /// 判断当前参数<typeparamref name="parameter"/>值是否大于等于0，否则抛出错误。
        /// </summary>
        /// <param name="parameter">参数值。</param>
        /// <param name="parameterName">参数名称。</param>
        public static void IsNotNegative(int parameter, string parameterName)
        {
            if (parameter < 0)
            {
                throw new ArgumentOutOfRangeException(parameterName, Resources.CannotBeNegative.FormatWith(new object[] { parameterName }));
            }
        }

        /// <summary>
        /// 判断当前参数<typeparamref name="parameter"/>值是否大于等于0，否则抛出错误。
        /// </summary>
        /// <param name="parameter">参数值。</param>
        /// <param name="parameterName">参数名称。</param>
        public static void IsNotNegative(float parameter, string parameterName)
        {
            if (parameter < 0f)
            {
                throw new ArgumentOutOfRangeException(parameterName, Resources.CannotBeNegative.FormatWith(new object[] { parameterName }));
            }
        }

        /// <summary>
        /// 判断当前参数<typeparamref name="parameter"/>值不能为空，否则抛出错误。
        /// </summary>
        /// <param name="parameter">参数值。</param>
        /// <param name="parameterName">参数名称。</param>
        public static void IsNotNull(object parameter, string parameterName)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(parameterName, Resources.CannotBeNull.FormatWith(new object[] { parameterName }));
            }
        }

        /// <summary>
        /// 判断当前参数<typeparamref name="parameter"/>值不能为空，并且数组元素个数大于0，否则抛出错误。
        /// </summary>
        /// <param name="parameter">参数值。</param>
        /// <param name="parameterName">参数名称。</param>
        public static void IsNotNullOrEmpty<T>(T[] parameter, string parameterName)
        {
            IsNotNull(parameter, parameterName);
            if (parameter.Length == 0)
            {
                throw new ArgumentException(Resources.ArrayCannotBeEmpty.FormatWith(new object[] { parameterName }));
            }
        }

        /// <summary>
        /// 判断当前参数<typeparamref name="parameter"/>值不能为空，并且集合元素个数大于0，否则抛出错误。
        /// </summary>
        /// <param name="parameter">参数值。</param>
        /// <param name="parameterName">参数名称。</param>
        public static void IsNotNullOrEmpty<T>(ICollection<T> parameter, string parameterName)
        {
            IsNotNull(parameter, parameterName);
            if (parameter.Count == 0)
            {
                throw new ArgumentException(Resources.CollectionCannotBeEmpty.FormatWith(new object[] { parameterName }), parameterName);
            }
        }

        /// <summary>
        /// 判断当前参数<typeparamref name="parameter"/>值不能为空或“ ”，否则抛出错误。
        /// </summary>
        /// <param name="parameter">参数值。</param>
        /// <param name="parameterName">参数名称。</param>
        public static void IsNotNullOrEmpty(string parameter, string parameterName)
        {
            if (string.IsNullOrEmpty(parameter))
            {
                throw new ArgumentException(Resources.CannotBeNullOrEmpty.FormatWith(new object[] { parameterName }));
            }
        }

        /// <summary>
        /// 判断当前参数<typeparamref name="parameter"/>是否为虚拟路径（以“~/”开头），否则抛出错误。
        /// </summary>
        /// <param name="parameter">参数值。</param>
        /// <param name="parameterName">参数名称。</param>
        public static void IsNotVirtualPath(string parameter, string parameterName)
        {
            IsNotNullOrEmpty(parameter, parameterName);
            if (!parameter.StartsWith("~/", StringComparison.Ordinal))
            {
                throw new ArgumentException(Resources.SourceMustBeAVirtualPathWhichShouldStartsWithTileAndSlash, parameterName);
            }
        }

        /// <summary>
        /// 判断当前参数<typeparamref name="parameter"/>值是否大于0，否则抛出错误。
        /// </summary>
        /// <param name="parameter">参数值。</param>
        /// <param name="parameterName">参数名称。</param>
        public static void IsNotZeroOrNegative(int parameter, string parameterName)
        {
            if (parameter <= 0)
            {
                throw new ArgumentOutOfRangeException(parameterName, Resources.CannotBeNegativeOrZero.FormatWith(new object[] { parameterName }));
            }
        }
    }
}
